package com.xin.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.xin.entity.STeacherRole;
import com.xin.entity.STeacherRoleRelation;
import com.xin.mapper.STeacherRoleRelationDao;
import com.xin.service.STeacherRoleRelationService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xin.service.STeacherRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 角色关系表 服务实现类
 * </p>
 *
 * @author l1
 * @since 2017-12-05
 */
@Service
public class STeacherRoleRelationServiceImpl extends ServiceImpl<STeacherRoleRelationDao, STeacherRoleRelation> implements STeacherRoleRelationService {

    @Autowired
    private STeacherRoleService sTeacherRoleService;
    @Override
    public JSONObject query(String roleId, String name, String eid, String cid, Integer pageSize, Integer pageNum) {

        JSONObject result = new JSONObject();
        JSONObject param = new JSONObject();
        param.put("roleId",roleId);
        param.put("name",name);
        param.put("eid",eid);
        param.put("cid",cid);

        //每页显示的条数,做为接口参数传入
        if(pageSize==null){
            pageSize = 15;
        }
        param.put("pageSize",pageSize);

        if(pageNum== null){
            pageNum = 1;
        }
        int offset = (pageNum-1) * pageSize;
        param.put("offset",offset);
        result.put("pageNum",pageNum);

        //总的条数，从数据库中查询
        int totalCount = baseMapper.queryRoleCount(param);
        result.put("totalCount",totalCount);
        List<JSONObject> resultList = baseMapper.queryRole(param);
        result.put("list",resultList);
        //总页数
        double tp= totalCount / (double)pageSize;
        int totalPage = (int) Math.ceil(tp);
        result.put("totalPage",totalPage);

        //计算开始 结束
        int start = pageNum-4;
        int end = pageNum+5;
        if(end<=totalPage){
            result.put("end",end);
            if(start>0){
                result.put("start",start);
            }else {
                result.put("start",1);
            }
        }else {
            result.put("end",totalPage);
            if(start>0){
                result.put("start",start);
            }else {
                result.put("start",1);
            }
        }
        return result;
    }

    @Override
    public List<JSONObject> queryRoles(String teacherId) {
        List<STeacherRole> sTeacherRoles = sTeacherRoleService.selectList(null);
        List<Integer> roleIds = baseMapper.queryRoleByTeacherId(teacherId);
        List<JSONObject> roles = new ArrayList<>();
        sTeacherRoles.forEach(sTeacherRole->{
            JSONObject role = new JSONObject();
            Integer id = sTeacherRole.getId();
            role.put("roleId",id);
            role.put("roleName",sTeacherRole.getRoleName());
            if(roleIds.contains(id)){
                role.put("checked",true);
            }else {
                role.put("checked",false);
            }
            roles.add(role);

        });
        return roles;
    }

    @Transient
    @Override
    public void updateRelation(String teacherId, String roleId) {
        //新增
        STeacherRoleRelation strr = new STeacherRoleRelation();
        strr.setTeacherId(teacherId);
        baseMapper.delete(new EntityWrapper<>(strr));
        strr.setRoleId(Integer.valueOf(roleId));
        baseMapper.insert(strr);
    }
}
